package com.lxj.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lxj.common.Result;
import com.lxj.domain.BusCustomer;
import com.lxj.dto.BusCustomDto;
import com.lxj.mapper.BusCustomerMapper;
import com.lxj.service.CustomService;
import com.lxj.vo.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author lxj
 * @date 2021/6/21 15:56
 * @Description:
 */
@Service
public class CustomServiceImpl implements CustomService {

    @Autowired
    private BusCustomerMapper mapper;

    @Override
    public PageInfo<BusCustomer> findByPage(BusCustomDto dto) {
        Page<BusCustomer> page = PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<BusCustomer> customers = mapper.selectAll(dto);
        return new PageInfo<>(page.getTotal(),customers);
    }

    @Override
    @Transactional
    public Result add(BusCustomDto dto) {
        BusCustomer busCustomer = new BusCustomer();
        BeanUtils.copyProperties(dto, busCustomer);
        //填充默认的字段
        busCustomer.setCreateTime(new Date());
        busCustomer.setUpdateTime(new Date());
        mapper.insert(busCustomer);
        return new Result();
    }

    @Override
    @Transactional
    public Result batchDelete(Integer[] ids) {
        mapper.batchDelete(ids);
        return new Result();
    }

    @Override
    @Transactional
    public Result updateById(BusCustomDto dto) {
        BusCustomer busCustomer = new BusCustomer();
        BeanUtils.copyProperties(dto, busCustomer);
        mapper.updateByPrimaryKeySelective(busCustomer);
        return new Result();
    }
}
